﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Xpedite.Extensions;
using Xpedite.Resources;
using Xpedite.Resources.Builders;
using Xpedite.Resources.Combiners;
using Xpedite.Resources.Definitions;
using Xpedite.WebAbstraction;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Test.Extensions
{
  [TestClass]
  public class WebContextExtensionsTest
  {
    private readonly Mock<IWebContext> _context = new Mock<IWebContext>(MockBehavior.Strict);


    [TestMethod]
    public void TestGetResourceBuilderFactoryWhereDefinitionArgsCached()
    {
      var args = ExpectDefinitionArgsCached();

      Assert.AreSame(args.BuilderFactory, _context.Object.GetResourceBuilderFactory());
    }

    [TestMethod]
    public void TestGetResourceBuilderFactoryWhereDefinitionArgsNotCached()
    {
      var capturedArgs = ExpectDefinitionArgsNotCached();

      var builderFactory = _context.Object.GetResourceBuilderFactory();

      Assert.AreSame(capturedArgs.Value.BuilderFactory, builderFactory);
    }
  
    [TestMethod]
    public void TestGetResourceDefinitionFactoryWhereFactoryCached()
    {
      var definitionArgs = CreateDefinitionArgs();
      var definitionFactory = new ResourceDefinitionFactory(definitionArgs);

      _context.SetupGet(mock => mock[typeof(ResourceDefinitionFactory).AssemblyQualifiedName]).Returns(definitionFactory);

      Assert.AreSame(definitionFactory, _context.Object.GetResourceDefinitionFactory());
    }

    [TestMethod]
    public void TestGetResourceDefinitionFactoryWhereFactoryNotCached()
    {
      var definitionArgs = ExpectDefinitionArgsCached();
      var capturedArgs = new CapturedValue<IResourceDefinitionFactory>();

      _context.SetupGet(mock => mock[typeof(ResourceDefinitionFactory).AssemblyQualifiedName]).Returns(null);
      _context.SetupSet(mock => mock[typeof(ResourceDefinitionFactory).AssemblyQualifiedName] = It.Is(capturedArgs.Match));

      var definitionFactory =  _context.Object.GetResourceDefinitionFactory();

      Assert.AreSame(capturedArgs.Value, definitionFactory);
    }
    
    [TestMethod]
    public void TestGetResourceCacheWhereDefinitionArgsCached()
    {
      var args = ExpectDefinitionArgsCached();

      Assert.AreSame(args.ResourceCache, _context.Object.GetResourceCache());
    }

    [TestMethod]
    public void TestGetResourceCacheWhereDefinitionArgsNotCached()
    {
      var capturedArgs = ExpectDefinitionArgsNotCached();
      
      var resourceCache = _context.Object.GetResourceCache();

      Assert.AreSame(capturedArgs.Value.ResourceCache, resourceCache);
    }

    private DefinitionArgs ExpectDefinitionArgsCached()
    {
      var definitionArgs = CreateDefinitionArgs();

      _context.SetupGet(mock => mock[typeof(DefinitionArgs).AssemblyQualifiedName]).Returns(definitionArgs);

      return definitionArgs;
    }

    private CapturedValue<DefinitionArgs> ExpectDefinitionArgsNotCached()
    {
      var capturedArgs = new CapturedValue<DefinitionArgs>();
      var webCache = new Mock<IWebCache>(MockBehavior.Strict);
      
      _context.SetupGet(mock => mock[typeof(DefinitionArgs).AssemblyQualifiedName]).Returns(null);
      _context.SetupGet(mock => mock.Cache).Returns(webCache.Object);
      _context.SetupSet(mock => mock[typeof(DefinitionArgs).AssemblyQualifiedName] = It.Is(capturedArgs.Match));

      return capturedArgs;
    }

    private DefinitionArgs CreateDefinitionArgs()
    {
      var resourceCache = new Mock<IResourceCache>(MockBehavior.Strict);
      var combinerFactory = new Mock<ICombinerFactory>(MockBehavior.Strict);
      var urlGenerator = new Mock<IResourceUrlGenerator>(MockBehavior.Strict);
      var builderFactory = new Mock<IResourceBuilderFactory>(MockBehavior.Strict);
      
      return new DefinitionArgs(resourceCache.Object, urlGenerator.Object, builderFactory.Object, combinerFactory.Object);
    }
  }
}
